package com.zeros.myself.practice_delete.practice7.sample;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.zeros.myself.R;

/**
 * @author zxx on 2020/10/13
 */
public class Sample05AnimatorSetLayout extends RelativeLayout {

    public Sample05AnimatorSetLayout(Context context) {
        super(context);
    }

    public Sample05AnimatorSetLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Sample05AnimatorSetLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    View view;
    Button animateBt;

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        view = findViewById(R.id.objectAnimatorView);
        animateBt = findViewById(R.id.animateBt);

        animateBt.setOnClickListener(v -> {
            view.setTranslationX(-200f);
            ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
            ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "translationX", -200, 200);
            ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "rotation", 0, 1080);
            animator3.setDuration(1000);

            // 用 AnimatorSet 的方法来让三个动画协作执行
            // 要求 1： animator1 先执行，animator2 在 animator1 完成后立即开始
            // 要求 2： animator2 和 animator3 同时开始
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.play(animator1).before(animator2);  // 先执行 1 再执行 2
            animatorSet.playTogether(animator2, animator3); // 2 和 3 同时开始

            animatorSet.start();

        });

    }
}
